﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Windows.Browser;
using System.Xml;
using System.Xml.Linq;
using System.ServiceModel.Syndication;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;

namespace PicasaManager
{
    public partial class MainPage : UserControl
    {
        ObservableCollection<string> urls;
        GAdapter adapter = new GAdapter();
        public MainPage()
        {

            InitializeComponent();
            GooglePicasaUser user = new GooglePicasaUser();
         
            this.DataContext = user;

            if (Application.Current.IsRunningOutOfBrowser)
            {
                urls = new ObservableCollection<string>();
                itemsList.ItemsSource = urls;

               
                adapter.LoginAsync("http://picasaweb.google.com/data/", (response) =>
                {
                    //adapter.RequestUserWithAlbums(txtUserName.Text, (xmlStream) =>
                    //    {
                    //        Dispatcher.BeginInvoke(() =>
                    //        {
                    //            //user = GooglePicasaUser.FromXmlStream(xmlStream);
                    //            //itemsList.ItemsSource = user.Albums;
                    //        });
                    //    });
                    //adapter.RequestContacts("raul.andrisan", (contactsXml) =>
                    //    {
                    //    });
                    //adapter.RequestSearch("dog", (searchXml) =>
                    //    {

                    //    });
                    adapter.RequestFeatured(20,(featuredXml) =>
                        {
                        });
                //    adapter.RequestFeatured((xmlStream) =>
                //        {
                //            Dispatcher.BeginInvoke(() =>
                //            {
                //                var a = GoogleAlbum.FromXmlStream(xmlStream);
                //                itemsList.ItemsSource = a.Photos;
                //                //user = GooglePicasaUser.FromXmlStream(xmlStream);
                //                //itemsList.ItemsSource = user.Albums;
                //            });
                //        });

                });
            }
        }

        private void OnPictureLoaded(object sender, RoutedEventArgs e)
        {
            if ((sender as FrameworkElement).Tag is GoogleAlbum)
            {
                var album = (GoogleAlbum)(sender as FrameworkElement).Tag;

                adapter.RequestAlbum(album.Author, album.Id, (stream) =>
                    {
                        Dispatcher.BeginInvoke(() =>
                             {
                                 var a = GoogleAlbum.FromXmlStream(stream);
                                 itemsList.ItemsSource = a.Photos;
                             });
                    });
            }
            else
            {
                var photo = (GooglePhoto)(sender as FrameworkElement).Tag;

                adapter.RequestPhoto("raul.andrisan",photo.AlbumId,photo.Id, (stream) =>
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                       
                    });
                });
            }
        }

        private void OnBack(object sender, RoutedEventArgs e)
        {
            adapter.RequestUserWithAlbums(txtUserName.Text, (xmlStream) =>
            {
                Dispatcher.BeginInvoke(() =>
                {
                    var user = GooglePicasaUser.FromXmlStream(xmlStream);
                    itemsList.ItemsSource = user.Albums;
                });
            });
        }




    }
}
